package fact.view.controller;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpSession;

import fact.view.DTO.UserDTO;
import fact.view.model.UserModelBean;
import fact.view.util.FacesUtils;
import fact.view.util.ServletUtil;


@ManagedBean(name = "userBean")
@RequestScoped
public class UserBean extends ServletUtil implements Serializable {

	private static final long serialVersionUID = 8292853620260831727L;
	private boolean opened = false;
	private boolean editOpened = false;
	
	
	@ManagedProperty(value="#{userModelBean}")
	private UserModelBean userModelBean;
	
	@ManagedProperty(value="#{loginBean}")
	private LoginBean loginBean;
	
	private SelectItem[] userSelectList;
	
	
	ArrayList<UserDTO> userList=new  ArrayList<UserDTO>();

	private UserDTO user=new UserDTO();
	private UserDTO editUser=new UserDTO();
	
		 
    HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		
	public String addUser(ActionEvent event) throws SQLException
	{
		opened=false;
		editOpened=false;
		
		if(this.user.getUserId()==null || this.user.getUserId().trim().equalsIgnoreCase(""))
		{
			opened=true;
			
			FacesUtils.addErrorMessage("form:userId","User ID cannot be empty.");
		}
		if(this.user.getPassword()==null || this.user.getPassword().trim().equalsIgnoreCase(""))
		{
			opened=true;
			
			FacesUtils.addErrorMessage("form:userPassword","Password cannot be empty.");
		}
		if(this.user.getUserName()==null || this.user.getUserName().trim().equalsIgnoreCase(""))
		{
			opened=true;
			
			FacesUtils.addErrorMessage("form:userName","User Name cannot be empty.");
		}
		
		if(this.user.getIsActive()==null || this.user.getIsActive().equalsIgnoreCase(""))
		{
			opened=true;			
			FacesUtils.addErrorMessage("form:userIsActive","Please select active/inactive");
		}
		
		if(this.user.getUserRoleId()==0)
		{
			opened=true;			
			FacesUtils.addErrorMessage("form:userRoleId","Please select user Role");
		}
		
		
		if(opened==false)
		{
			if(userModelBean.getUser(this.user.getUserId().trim())!=null)
			{
				opened=true;
				
				FacesUtils.addErrorMessage("form:userId","User Id Already Exist.");
			}
			else
			{
				if(userModelBean.addUser(user,loginBean.getUserBean().getUserId())==true)
				{
					userList=userModelBean.getUserList();
					session.setAttribute("session_userList", userList);
					
					FacesUtils.addInfoMessage(null,"User has successfully been added.");
					return "admin/user.jsf";
	
				}
				
			}
		}
		return null;
	}
	
	public String deleteUser() throws SQLException
	{
		System.out.println("User Id :"+this.user.getUserId());
		if(userModelBean.deleteUser(this.user.getUserId())==true)
		{
			userList=userModelBean.getUserList();
			session.setAttribute("session_userList", userList);
			
			FacesUtils.addInfoMessage(null,"User has successfully been deleted.");
		}
		return "admin/user.jsf";
	}
	
	 
	public String editUser() throws SQLException
	{
		editOpened=false;
		opened=false;
		
		if(this.editUser.getPassword()==null || this.editUser.getPassword().trim().equalsIgnoreCase(""))
		{
			editOpened=true;
			
			FacesUtils.addErrorMessage("form:editUserPassword","Password cannot be empty.");
		}
		if(this.editUser.getUserName()==null || this.editUser.getUserName().trim().equalsIgnoreCase(""))
		{
			editOpened=true;
			
			FacesUtils.addErrorMessage("form:editUserName","User Name cannot be empty.");
		}
		if(this.editUser.getIsActive()==null || this.editUser.getIsActive().equalsIgnoreCase(""))
		{
			editOpened=true;			
			FacesUtils.addErrorMessage("form:editUserIsActive","Please select active/inactive");
		}
		
		if(this.editUser.getUserRoleId()==0)
		{
			editOpened=true;			
			FacesUtils.addErrorMessage("form:editUserRoleId","Please select user Role");
		}
		
		
		if(editOpened==false)
		{
			System.out.println("User Id :"+this.editUser.getUserId());
			if(userModelBean.editUser(editUser)==true)
			{
				userList=userModelBean.getUserList();
				session.setAttribute("session_userList", userList);
				
				FacesUtils.addInfoMessage(null,"User has successfully been edited.");
			}
			return "admin/user.jsf";
		}
		return null;
	}

	

	public UserModelBean getUserModelBean() {
		return userModelBean;
	}

	public void setUserModelBean(UserModelBean userModelBean) {
		this.userModelBean = userModelBean;
	}

	public ArrayList<UserDTO> getUserList() {
		try{
			
			if(session.getAttribute("session_userList")==null)
			{
				userList=userModelBean.getUserList();
				session.setAttribute("session_userList", userList);
			}
			else
				userList=(ArrayList<UserDTO>)session.getAttribute("session_userList");
		   }
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		
	
		return userList;
	}
	

	public UserDTO getUser() {
		return user;
	}

	public void setUser(UserDTO user) {
		this.user = user;
	}

	public UserDTO getEditUser() {
		return editUser;
	}

	public void setEditUser(UserDTO editUser) {
		this.editUser = editUser;
	}

	public void setUserList(ArrayList<UserDTO> userList) {
		this.userList = userList;
	}

	public LoginBean getLoginBean() {
		return loginBean;
	}

	public void setLoginBean(LoginBean loginBean) {
		this.loginBean = loginBean;
	}

	
	public SelectItem[] getUserSelectList() {
		
	
		if(userList.size()==0)
			getUserList();
		
		this.userSelectList = new SelectItem[userList.size()];
		 for(int i=0;i<userList.size();i++)
			 userSelectList[i] = new SelectItem(userList.get(i).getUserId(), userList.get(i).getUserName());
		
		 
		 
		return userSelectList;
	}

	public void setUserSelectList(SelectItem[] userSelectList) {
		this.userSelectList = userSelectList;
	}

	/** -- Popup Modal Event ---**/
		public boolean isOpened() { return opened; }
		public boolean isEditOpened() { return editOpened; }
	    
	    public void setOpened(boolean opened) { this.opened = opened; }
	    public void setEditOpened(boolean editOpened) { this.editOpened = editOpened; }
	    
	    
	    public void openEvent(ActionEvent event) {
	    	this.user=null;
	        opened = true;
	        editOpened = false;
	    }
	    public void openEditEvent(ActionEvent event)  throws SQLException{
	    	this.editUser=userModelBean.getUser(this.editUser.getUserId());
	    	editUser.setIsActive(editUser.getIsActive().equalsIgnoreCase("Active")?"1":"0");
	    	editOpened = true;
	    	opened = false;
	    }
	    
	    
	    public void closeEvent(ActionEvent event) {
	        opened = false;
	        editOpened = false;
	    }

    /*  End of Modal */
	

}